﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditCondition : FormPage
    {
        private Lc_ConditionBLL lcb = new Lc_ConditionBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Lc_QueueBLL lqb = new Lc_QueueBLL();

        protected override void BindPage()
        {
            BindProcess();
            if (FORM_IFEDIT)
            {
                Lc_Condition model = lcb.GetInfoById(FORM_KEY);
                txtName.Text = model.C_CONDITION_NAME;
                ddlProcess.SelectedValue = model.N_PROCESS_ID.ToString();
                Change();
                ddlJiedianLeft.SelectedValue = model.N_QUEUE_ID_LEFT.ToString();
                ddlJiedianRigth.SelectedValue = model.N_QUEUE_ID_RIGHT.ToString();
                ddlState.SelectedValue = model.N_CONDITION_STATE.ToString();
            }
            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void BindProcess()
        {
            // DataTable dt = lcb.GetAllInfo("", "");
            DataTable dt = lpb.GetAllInfo("", "");
            ddlProcess.DataSource = dt;
            ddlProcess.DataValueField = "N_PROCESS_ID";
            ddlProcess.DataTextField = "C_PROCESS_NAME";
            ddlProcess.DataBind();
            ListItem item = new ListItem("请选择", "-1");
            ddlProcess.Items.Insert(0, item);
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show(Page, "逻辑名称不能为空");
                return;
            }
            if (ddlProcess.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }
            if (ddlJiedianLeft.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择左节点");
                return;
            }
            if (ddlJiedianRigth.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择右节点");
                return;
            }
            if (ddlState.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择条件状态");
                return;
            }

            Lc_Condition model = new Lc_Condition();
            model.C_CONDITION_NAME = txtName.Text;
            model.N_PROCESS_ID = decimal.Parse(ddlProcess.SelectedValue);
            model.N_QUEUE_ID_LEFT = decimal.Parse(ddlJiedianLeft.SelectedValue);
            model.N_QUEUE_ID_RIGHT = decimal.Parse(ddlJiedianRigth.SelectedValue);
            model.N_CONDITION_STATE = decimal.Parse(ddlState.SelectedValue);
            model.D_CONDITION_OPERATOR_TIME = DateTime.Now;
            model.N_CONDITION_OPERATOR_ID = CurrentUserID;
            if (FORM_IFEDIT)
            {
                model.N_CONDITION_ID = Convert.ToDecimal(FORM_KEY);
                if (lcb.Update(model, lognUser.USER_NAME))
                {
                    MessageBox.Show(Page, "保存成功");
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                }
            }
            else
            {
                if (lcb.Insert(model, lognUser.USER_NAME))
                {
                    MessageBox.Show(Page, "新增成功");
                    //FORM_IFEDIT = true;
                    //FORM_KEY = model.N_CONDITION_ID.ToString().Trim();
                    //var Builder = new StringBuilder();
                    //Builder.AppendLine("   var option={");
                    //Builder.AppendLine("                Msg:\"【温馨提示】添加成功！\",");
                    //Builder.AppendLine("                YesCallBack:function(index){");
                    //Builder.AppendLine("                                FORM_UPDATE_TITLE(\"编辑队列条件\");");
                    //Builder.AppendLine("                                top.layer.close(index);");
                    //Builder.AppendLine("                }");
                    //Builder.AppendLine("   };");
                    //Builder.AppendLine("   top.MessageBox.Alert(option);");
                    //MessageBox.ResponseScript(this, Builder.ToString());
                }
                else
                {
                    MessageBox.Show(Page, "新增失败");
                }
            }
        }

        protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
        {
            Change();
        }

        private void Change()
        {
            string pid = ddlProcess.SelectedValue;
            DataTable dt = lqb.GetAllInfo("ProcessID", pid);
            ddlJiedianLeft.Items.Clear();
            ddlJiedianRigth.Items.Clear();
            ddlJiedianRigth.DataSource = dt;
            ddlJiedianRigth.DataTextField = "C_NODE_NAME";
            ddlJiedianRigth.DataValueField = "N_QUEUE_ID";
            ddlJiedianRigth.DataBind();
            ListItem item1 = new ListItem("请选择", "-1");
            ddlJiedianRigth.Items.Insert(0, item1);

            ddlJiedianLeft.DataSource = dt;
            ddlJiedianLeft.DataTextField = "C_NODE_NAME";
            ddlJiedianLeft.DataValueField = "N_QUEUE_ID";
            ddlJiedianLeft.DataBind();
            ListItem item2 = new ListItem("请选择", "-1");
            ddlJiedianLeft.Items.Insert(0, item2);
        }
    }
}